package pers.xukai.fullMark.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.xukai.common.utils.HttpUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * @author : 殷绪凯
 * @description : 验证码生成
 * @since : 2022年 12月 02日 11:05
 */
@RestController
@RequestMapping("captcha")
public class CaptchaController {

    @GetMapping("/lineCaptcha")
    public void lineCaptcha(HttpServletResponse httpServletResponse) throws IOException {
        //定义图形验证码的长、宽、验证码字符数、干扰线宽度
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 4);
        //ShearCaptcha captcha = new ShearCaptcha(200, 100, 4, 4);
        //图形验证码写出，可以写出到文件，也可以写出到流
        byte[] imageBytes = captcha.getImageBytes();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageBytes);
        HttpUtil.writeStreamToResponse(httpServletResponse,"a",".png",byteArrayInputStream,"image/png;charset=UTF-8");
        //验证图形验证码的有效性，返回boolean值
        captcha.verify("1234");
    }
}
